import Login from "@/stores/login"
import FormData from "./formData"
class Axios {
    baseUrl: string
    loadNum: number
    constructor() {
        this.baseUrl = import.meta.env.VITE_APP_BASE_URL
        this.loadNum = 0
    }
    init<T>(url: string, data: any, option: { type: 'POST' | 'GET' | 'FILE', toast?: boolean | undefined, fileNames?: Array<string> }) {
        const login = Login(), toast = (msg: string) => setTimeout(() => uni.showToast({ title: msg, icon: 'none', duration: 3000 }), 350)
        return new Promise<T>((resolve, reject) => {
            // 请求动画
            let numTime: number, completeCall = () => { }
            numTime = setTimeout(() => {
                completeCall = () => {
                    if (this.loadNum > 0) this.loadNum--;
                    if (this.loadNum == 0) uni.hideLoading();
                }
                this.loadNum++
                uni.showLoading({
                    title: '请求中...'
                })
            }, 300)
            // 请求成功
            let success = (res: any) => {
                if (res.data.code == 200) {
                    resolve(res.data.data);
                    if (option.toast == true) toast(res.data.msg);
                } else if (res.data.code == 202) {
                    uni.redirectTo({
                        url: '/pages/login/login'
                    })
                    toast('请重新登录！')
                } else if (res.data.code == 205) {
                    uni.redirectTo({
                        url: '/pages/authentication/authentication'
                    })
                    toast(res.data.msg)
                } else {
                    reject(res.errMsg)
                    if (option.toast !== false) toast(res.data.msg);
                }
            }
            // 请求失败
            let fail = (err: any) => {
                reject(err)
                toast(err.errMsg)
            }
            // 请求完成
            let complete = (res: any) => {
                clearTimeout(numTime)
                completeCall()
            }
            if (option.type == 'FILE') {
                let formData = new FormData()
                if (option.fileNames) {
                    for (let i in data) {
                        if (option.fileNames.includes(i) && data[i]) {
                            formData.appendFile(i, data[i])
                        } else {
                            formData.append(i, data[i])
                        }
                    }
                } else {
                    for (let i in data) {
                        formData.append(i, data[i])
                    }
                }
                let uploadData = formData.getData()
                uni.request({
                    url: this.baseUrl + url,
                    data: uploadData.buffer,
                    method: 'POST',
                    header: {
                        'token': login.token,
                        'Content-Type': uploadData.contentType
                    },
                    success,
                    fail,
                    complete
                })
            } else {
                uni.request({
                    url: this.baseUrl + url,
                    data: data,
                    method: option.type,
                    header: {
                        'token': login.token
                    },
                    success,
                    fail,
                    complete
                })
            }
        })
    }
    post<T>(url: string, data: object, toast?: boolean) {
        return this.init<T>(url, data, {
            type: 'POST',
            toast
        })
    }
    get<T>(url: string, data?: object, toast?: boolean) {
        return this.init<T>(url, data || undefined, { type: 'GET', toast })
    }
    file<T>(url: string, data: object, option: object) {
        return this.init<T>(url, data, { type: 'FILE', ...option })
    }

}
export default new Axios();